//
//  SZRegisterDetailViewController.m
//  KaiJing
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 HuangZuCui. All rights reserved.
//

#import "SZRegisterDetailViewController.h"
#import "PKRequestManager.h"

// 点击完成注册资料
#define kRegisterURL @"http://morguo.com/member.php?mod=register&androidflag=1&appfrom=ios&iosversion=3.13&mobile=0"

@interface SZRegisterDetailViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
/** 头像button */
@property (weak, nonatomic) IBOutlet UIButton *iconButton;
/** 用户名 */
@property (weak, nonatomic) IBOutlet UITextField *username;
/** 密码 */
@property (weak, nonatomic) IBOutlet UITextField *password;

@property (nonatomic, strong) UIView *baseView;
@property (nonatomic, strong) UIImagePickerController *picker;
@property (nonatomic, strong) UIImage *iconImage;
@end

@implementation SZRegisterDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _iconButton.layer.cornerRadius = 40;
    _iconButton.layer.masksToBounds = YES;
    
    _picker = [[UIImagePickerController alloc] init];
    _picker.delegate = self;
    _picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    _picker.allowsEditing = YES;
}

/**
 *  点击头像选择图片
 */
- (IBAction)iconBtnAction {// 添加相册还是照相按钮
    if (_baseView != nil) {
        [self.view bringSubviewToFront:_baseView];
        return;
    }
    _baseView = [[UIView alloc] initWithFrame:self.view.bounds];
    _baseView.backgroundColor = [UIColor grayColor];
    _baseView.alpha = 0.4;
    [self.view addSubview:_baseView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [_baseView addGestureRecognizer:tap];
    UIButton *photoButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    photoButton.frame = CGRectMake(kScreenWidth / 2 - 40, kScreenHeight / 2 - 30, 80, 30);
    [photoButton setTitle:@"相机" forState:(UIControlStateNormal)];
    [photoButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [photoButton addTarget:self action:@selector(photoAction) forControlEvents:(UIControlEventTouchUpInside)];
    [photoButton setBackgroundColor:[UIColor whiteColor]];
    [_baseView addSubview:photoButton];
    
    UIButton *photoListButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    photoListButton.frame = CGRectMake(kScreenWidth / 2 - 60, kScreenHeight / 2, 120, 30);
    [photoListButton setTitle:@"从相册选择" forState:(UIControlStateNormal)];
    [photoListButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [photoListButton addTarget:self action:@selector(photoListAction) forControlEvents:(UIControlEventTouchUpInside)];
    [photoListButton setBackgroundColor:[UIColor whiteColor]];
    [_baseView addSubview:photoListButton];
}

/**
 *  点击完成,注册信息
 */
- (IBAction)finishActon {
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[_registerBaseModel.password2] = _password.text;
    parDic[@"formhash"] = _registerBaseModel.formhash;
    parDic[@"referer"] = _registerBaseModel.referer;
    parDic[_registerBaseModel.phone] = _phoneNumber;
    parDic[@"regsubmit"] = _registerBaseModel.regsubmit;
    parDic[_registerBaseModel.password] = _password.text;
    parDic[_registerBaseModel.username] = _username.text;
    [PKRequestManager requestWithUrlString:kRegisterURL parDic:parDic method:POST finish:^(NSDictionary *dataDic) {
        SZLog(@"%@",dataDic);
        // 判断是否注册成功
    } error:^(NSError *error) {
        SZLog(@"%@",error);
    }];
}

- (void)photoAction {
    //    _picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    //    [self presentViewController:_picker animated:YES completion:nil];
}
- (void)photoListAction {
    _picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentViewController:_picker animated:YES completion:nil];
}
- (void)tapAction:(UITapGestureRecognizer *)tap {
    [self.view sendSubviewToBack:_baseView];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    [_iconButton setImage:info[UIImagePickerControllerEditedImage] forState:(UIControlStateNormal)];
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        UIImageWriteToSavedPhotosAlbum(info[UIImagePickerControllerEditedImage], nil, nil, nil);
    }
    _iconImage = info[UIImagePickerControllerEditedImage];
    [self.view sendSubviewToBack:_baseView];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
